package com.zjuee.web.ledger.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.ledger.LedgerProduceBillDto;
import com.zjuee.base.dto.common.ledger.LedgerProduceGatherDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.ledger.LedgerProduce;
import com.zjuee.base.model.common.ledger.LedgerProduceApproval;
import com.zjuee.base.model.common.ledger.LedgerProduceBill;
import com.zjuee.base.model.common.ledger.LedgerProduceGather;
import com.zjuee.service.LedgerProduceApprovalService;
import com.zjuee.service.LedgerProduceBillService;
import com.zjuee.service.LedgerProduceGatherService;
import com.zjuee.service.LedgerProduceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_LEDGER_URL_PREFIX)
public class ProduceLedgerController {
    @Autowired
    LedgerProduceGatherService ledgerProduceGatherService;

    @Autowired
    LedgerProduceBillService ledgerProduceBillService;
    @Autowired
    LedgerProduceService ledgerProduceService;
    @Autowired
    LedgerProduceApprovalService ledgerProduceApprovalService;
    @Resource
    private LoginDto loginDto;

    //添加产生台账
    @PostMapping("/produce/gather/add")
    public R<Long> add(LedgerProduce ledgerProduce) {
        try {
            ledgerProduceService.add(ledgerProduce);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    @PostMapping("/produce/isInitLedgerProduce")
    public R<Map> isInitLedgerProduce(LedgerProduce ledgerProduce) {
        Map<String, Object> initLedgerProduce =new HashMap<>();
        try {
            initLedgerProduce=ledgerProduceService.isInitLedgerProduce(ledgerProduce);
        }catch (ServiceException e){
            return R.fail(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.ok(initLedgerProduce);
    }


    //库存变更
    @PostMapping("/produce/stock/change")
    public R<Long> stockChange(LedgerProduceApproval ledgerProduceApproval) {
        Long add = ledgerProduceApprovalService.add(ledgerProduceApproval);
        if (add != null) {
            return R.ok("提交成功!");
        }
        return R.fail("提交失败!");
    }

    //查询产生台账汇总信息列表
    @PostMapping("/produce/gather/list")
    public R<Page<LedgerProduceGather>> list(LedgerProduceGatherDto ledgerProduceGatherDto) {
        Page<LedgerProduceGather> list = ledgerProduceGatherService.list(ledgerProduceGatherDto);
        return R.ok(list);
    }
    //查询产生台账明细信息列表
    @PostMapping("/produce/bill/list")
    public R<Page<LedgerProduceBill>> billList(LedgerProduceBillDto ledgerProduceBillDto) {
        Page<LedgerProduceBill> list = ledgerProduceBillService.list(ledgerProduceBillDto);
        return R.ok(list);
    }
    //导出产生台账汇总信息
    @PostMapping("/produce/gather/excel")
    public void produceGatherExcel(LedgerProduceGather ledgerProduceGather) {
        ledgerProduceGatherService.exportExcel(ledgerProduceGather);
    }
    //导出产生台账明细信息
    @PostMapping("/produce/bill/excel")
    public void produceBillExcel(LedgerProduceBill ledgerProduceBill) {

    }
    //查询产生台账库存基本信息
    @PostMapping("/produce/gather/selectList")
    public R<List<LedgerProduceGather>> selectList(LedgerProduceGather ledgerProduceGather) {
        List<LedgerProduceGather> list = ledgerProduceGatherService.getList(ledgerProduceGather);
        return R.ok(list);
    }
}

